<?php

class totalController extends base
{
    public function __construct()
    {
        parent::__construct('admin');

        // 授权用户虎扑帐号ID
        $uids = array(
            'wanglin' => 16306719,
            'linchunmin' => 19245790,
            'yangxinyang' => 5152038,
            'caoliang' => 4232423,
            'gaoyanjing' => 26797105,
            'fangsiyi' => 28072686,
        );

        if(!in_array($this->realUser['uid'], $uids))
        {
            exit('<h3>这里什么都没有的!</h3>');
        }

        $this->in_db('totalDb');
    }

    public function doinit()
    {
        $this->doclientTotalNewDay();
    }

    public function doclientTotalNewDay()
    {
        $bt = intval(common::request('bt'));
        $et = intval(common::request('et'));

        $bts = $bt ? " and ymd>={$bt}" : '';
        $ets = $et ? " and ymd<={$et}" : '';

        $totalDays = $this->totalDb->getAll("select * from client_total_new_stay_day where 1=1 {$bts}{$ets} order by ymd desc limit 500");

        foreach($totalDays as $k => $v)
        {
            $totalDays[$k]['new'] = $v['new_android']+$v['new_ios'];
            $totalDays[$k]['active'] = $v['active_android']+$v['active_ios'];
            $totalDays[$k]['stay1'] = $v['stay1_android']+$v['stay1_ios'];
            $totalDays[$k]['stay2'] = $v['stay2_android']+$v['stay2_ios'];
            $totalDays[$k]['stay3'] = $v['stay3_android']+$v['stay3_ios'];
            $totalDays[$k]['stay4'] = $v['stay4_android']+$v['stay4_ios'];
            $totalDays[$k]['stay5'] = $v['stay5_android']+$v['stay5_ios'];
            $totalDays[$k]['stay6'] = $v['stay6_android']+$v['stay6_ios'];
            $totalDays[$k]['stay7'] = $v['stay7_android']+$v['stay7_ios'];

            for($i = 1; $i <= 7; $i++)
            {
                if($totalDays[$k]['new'])
                {
                    $totalDays[$k]["stay{$i}_rate"] = round(($totalDays[$k]["stay{$i}"] / $totalDays[$k]['new']) * 100, 2) . '%';
                }
                else
                {
                    $totalDays[$k]["stay{$i}_rate"] = '0%';
                }
            }
        }

        $this->view->assign('bt', !$bt ? '' : $bt);
        $this->view->assign('et', !$et ? '' : $et);
        $this->view->assign('totalDays', $totalDays);
        $this->view->display('total/clientTotalNewDay');
    }

    public function doclientTotalDay()
    {
        $bt = intval(common::request('bt'));
        $et = intval(common::request('et'));

        $bts = $bt ? " and ymd>={$bt}" : '';
        $ets = $et ? " and ymd<={$et}" : '';

        $totalDays = $this->totalDb->getAll("select * from client_total_active_stay_day where 1=1 {$bts}{$ets}  order by ymd desc limit 500");

        foreach($totalDays as $k => $v)
        {
            $totalDays[$k]['new'] = $v['new_android']+$v['new_ios'];
            $totalDays[$k]['active'] = $v['active_android']+$v['active_ios'];
            $totalDays[$k]['stay1'] = $v['stay1_android']+$v['stay1_ios'];
            $totalDays[$k]['stay2'] = $v['stay2_android']+$v['stay2_ios'];
            $totalDays[$k]['stay3'] = $v['stay3_android']+$v['stay3_ios'];
            $totalDays[$k]['stay4'] = $v['stay4_android']+$v['stay4_ios'];
            $totalDays[$k]['stay5'] = $v['stay5_android']+$v['stay5_ios'];
            $totalDays[$k]['stay6'] = $v['stay6_android']+$v['stay6_ios'];
            $totalDays[$k]['stay7'] = $v['stay7_android']+$v['stay7_ios'];

            for($i = 1; $i <= 7; $i++)
            {
                if($totalDays[$k]['new'])
                {
                    $totalDays[$k]["stay{$i}_rate"] = round(($totalDays[$k]["stay{$i}"] / $totalDays[$k]['new']) * 100, 2) . '%';
                }
                else
                {
                    $totalDays[$k]["stay{$i}_rate"] = '0%';
                }
            }
        }

        $this->view->assign('bt', !$bt ? '' : $bt);
        $this->view->assign('et', !$et ? '' : $et);
        $this->view->assign('totalDays', $totalDays);
        $this->view->display('total/clientTotalDay');
    }

    public function doclientTotalWeek()
    {
        $totalWeeks = $this->totalDb->getAll("select * from client_total_week order by yw desc limit 30");

        $this->view->assign('totalWeeks', $totalWeeks);
        $this->view->display('total/clientTotalWeek');
    }

    public function doclientTotalWeekDesc()
    {
        $totalWeeks = $this->totalDb->getAll("select * from client_total_active_week order by yw desc limit 30");

        $this->view->assign('totalWeeks', $totalWeeks);
        $this->view->display('total/clientTotalWeekDesc');
    }

    public function doclientTotalMonth()
    {
        $totalMonths = $this->totalDb->getAll("select * from client_total_month order by ym desc limit 30");

        $this->view->assign('totalMonths', $totalMonths);
        $this->view->display('total/clientTotalMonth');
    }

    public function doclientTotalMonthDesc()
    {
        $ym = intval(common::request('ym'));
        $totalMonths = $this->totalDb->getAll("select * from client_total_active_month where total_ym={$ym} order by id asc");
        $totalStay = 0;

        foreach($totalMonths as $k => $v)
        {
            $totalStay += $v['stay'];
        }

        foreach($totalMonths as $k => $v)
        {
            $totalMonths[$k]['stay_rate_month'] = round(($v['stay'] / $totalStay) * 100, 4) . '%';
        }

        $this->view->assign('totalStay', $totalStay);
        $this->view->assign('totalMonths', $totalMonths);
        $this->view->display('total/clientTotalMonthDesc');
    }
}